package cc.mrbird.febs.payment.service.impl;

import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.pay.enums.ALiTradeState;
import cc.mrbird.febs.pay.enums.WxRefundStatus;
import cc.mrbird.febs.payment.entity.PaymentInfo;
import cc.mrbird.febs.payment.entity.RefundInfo;
import cc.mrbird.febs.payment.mapper.RefundInfoMapper;
import cc.mrbird.febs.payment.service.IRefundInfoService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.commons.codec.binary.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;

/**
 * 退款记录 Service实现
 *
 * @author Mr.Yang
 * @date 2022-08-12 09:32:44
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundInfo> implements IRefundInfoService {

    @Autowired
    private RefundInfoMapper refundInfoMapper;

    @Override
    public IPage<RefundInfo> findRefundInfos(QueryRequest request, RefundInfo refundInfo) {
        LambdaQueryWrapper<RefundInfo> queryWrapper = new LambdaQueryWrapper<>();
        // TODO 设置查询条件
        Page<RefundInfo> page = new Page<>(request.getPageNum(), request.getPageSize());
        return this.page(page, queryWrapper);
    }

    @Override
    public List<RefundInfo> findRefundInfos(RefundInfo refundInfo) {
        LambdaQueryWrapper<RefundInfo> queryWrapper = new LambdaQueryWrapper<>();
        // TODO 设置查询条件
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional
    public void createRefundInfo(RefundInfo refundInfo) {
        this.save(refundInfo);
    }

    @Override
    @Transactional
    public void updateRefundInfo(RefundInfo refundInfo) {
        this.saveOrUpdate(refundInfo);
    }

    @Override
    @Transactional
    public void deleteRefundInfo(RefundInfo refundInfo) {
        LambdaQueryWrapper<RefundInfo> wrapper = new LambdaQueryWrapper<>();
        // TODO 设置删除条件
        this.remove(wrapper);
    }

    @Override
    public synchronized RefundInfo updateRefundContent(String orderSn, String content, String tradeState) {
        RefundInfo refundInfo = new RefundInfo().setUpdateTime(new Date());
        if (StringUtils.equals(tradeState.trim(), WxRefundStatus.SUCCESS.getType())) {
            refundInfo.setRefundStatus(WxRefundStatus.SUCCESS.getType());
        } else {
            refundInfo.setContentNotify(content);
        }
        refundInfoMapper.update(refundInfo, new LambdaUpdateWrapper<RefundInfo>().eq(RefundInfo::getOrderNo, orderSn));
        return null;
    }

}
